package com.tenglong.teacher.ui.setting;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangeSignActivity extends BaseActivity {

    private EditText mEditText;
    private TextView textNum;
    private View mConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_sign);
        initView();
        setListener();
        initData();
    }

    private void initData() {
        String sign = DataState.getInstance().getUserInfo().getSlogan();
        if (!TextUtils.isEmpty(sign)) {
            mEditText.setHint(sign);
        }
    }

    private void setListener() {
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textNum.setText(30 - s.toString().trim().length() + "");
                ediState();
            }
        });
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditText.getText().toString().trim().equals(DataState.getInstance().getUserInfo().getSlogan())) {
                    ToastUtils.showCenterToast(R.string.sign_modify_same);
                    return;
                }
                modifySign();
            }
        });
    }

    private void ediState() {
        boolean isClick = mEditText.getText().toString().trim().length() > 0;
        mConfirm.setEnabled(isClick);
        mConfirm.setAlpha(isClick ? 1 : (float) 0.5);
    }

    private void modifySign() {
        showProgressDialog(getString(R.string.sign_modifying));
        ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
        userInfo.setSlogan(mEditText.getText().toString().trim());
        CreateRequestEntity.getWebService()
                .modifyUserInfo(userInfo)
                .enqueue(new Callback<BaseResult<ClientUserInfo>>() {
                    @Override
                    public void onResponse(Call<BaseResult<ClientUserInfo>> call, Response<BaseResult<ClientUserInfo>> response) {
                        dismissProgressDialog();
                        if (ResultVerifier.isSucceed(response)) {
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResult<ClientUserInfo>> call, Throwable t) {
                        ResultVerifier.systerErrers(t);
                        dismissProgressDialog();
                    }
                });
    }

    private void initView() {
        setTitle(getString(R.string.sign_title));
        mEditText = findViewById(R.id.mEditText);
        textNum = findViewById(R.id.textNum);
        mConfirm = findViewById(R.id.right_btn);
        mConfirm.setVisibility(View.VISIBLE);
        ediState();
    }
}
